package interfaze;

import java.awt.Color;
import java.awt.Font;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import java.util.ArrayList;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

import constants.PercentNumber;

import logic.BBDataSaver;
import logic.Mp3Player;
import logic.NoSaveException;
import logic.Utilities;

/**Brick Breaker Main Menu Panel's class.
 * @author Team 1
 *
 */
@SuppressWarnings("serial")
public class BrickBreakerMainMenuPanel extends JPanel implements ActionListener, MouseListener {
	
	private SuperArcade2in1 window;											//The JFrame where this panel is being added.
	private JLabel background = new JLabel();								//The background of the panel.
	private JLabel fontTitle1 = new JLabel("Brick");						//One part of the title of the game.
	private JLabel fontTitle2 = new JLabel("Breaker");						//The other part of the title of the game.
	private Font font;														//The font that is used by all the buttons of the panel.
	private JButton bNewGame = new JButton("New Game");						//The New Game button.
	private JButton bContinue = new JButton("Continue");					//The Continue button.
	private JButton bInstructions = new JButton("Instructions");			//The Instructions button.
	private JButton bOptions = new JButton("Options");						//The Options button.
	private JButton bLeaderBoard = new JButton("Leader Board");				//The Leader Board button.
	private JButton bCredits = new JButton("Credits");						//The Credits button.
	private JButton bQuit = new JButton("Back");							//The Back button.
	
	/** BrickBreakerMainMenu's constructor.
	 * @param window : The JFrame where this panel is being added.
	 */
	public BrickBreakerMainMenuPanel(SuperArcade2in1 window) {
		this.window = window;
		
		//Sets the layout of the panel to null in order to work with absolute coordinates.
		this.setLayout(null);
		
		//Sets focusable and requests focus, trying to fix full screen bugs.
		this.setFocusable(true);
		this.requestFocus();
		
		//Sets the font that is going to be used for all the buttons.
		font = new Font("Helvetica", Font.PLAIN, Math.round(12 * window.xScale));
		
		//Sets the background of the panel.
		Image image = logic.Utilities.loadImage("res/background.png");
		if(window.xScale != 1 || window.yScale != 1) {
			image = Utilities.resizeImage(window.xScale, window.yScale, "res/background.png");
		}
		background.setIcon(new ImageIcon(image));
		background.setBounds(0, 0, image.getWidth(null), image.getHeight(null));
		
		//Sets all the components of the panel, the location
		fontTitle1.setBounds(Math.round(350 * window.xScale), Math.round(200 * window.yScale),
				Math.round(188 * window.xScale), Math.round(77 * window.yScale));
		fontTitle1.setFont(new Font("Helvetica", Font.BOLD, Math.round(75 * window.xScale)));
		fontTitle1.setForeground(Color.WHITE);
		fontTitle2.setBounds(Math.round(575 * window.xScale), Math.round(200 * window.yScale),
				Math.round(290 * window.xScale), Math.round(75 * window.yScale));
		fontTitle2.setFont(new Font("Helvetica", Font.BOLD, Math.round(75 * window.xScale)));
		fontTitle2.setForeground(new Color(0.0f, 0.70f, 1.0f));
		bNewGame.setBounds(Math.round(75 * window.xScale), Math.round(350 * window.yScale),
				Math.round(130 * window.xScale), Math.round(25 * window.yScale));
		bNewGame.setFont(font);
		bNewGame.addActionListener(this);
		bNewGame.addMouseListener(this);
		
		//If the save data doesn't exist disables the Continue button.
		if(!BBDataSaver.hasSave(window.userName)) {
			bContinue.setEnabled(false);
		}
		
		bContinue.setBounds(Math.round(100 * window.xScale), Math.round(380 * window.yScale),
				Math.round(130 * window.xScale), Math.round(25 * window.yScale));
		bContinue.setFont(font);
		bContinue.addActionListener(this);
		bContinue.addMouseListener(this);
		bInstructions.setBounds(Math.round(125 * window.xScale), Math.round(410 * window.yScale),
				Math.round(130 * window.xScale), Math.round(25 * window.yScale));
		bInstructions.setFont(font);
		bInstructions.addActionListener(this);
		bInstructions.addMouseListener(this);
		bOptions.setBounds(Math.round(150 * window.xScale), Math.round(440 * window.yScale),
				Math.round(130 * window.xScale), Math.round(25 * window.yScale));
		bOptions.setFont(font);
		bOptions.addActionListener(this);
		bOptions.addMouseListener(this);
		bLeaderBoard.setBounds(Math.round(175 * window.xScale), Math.round(470 * window.yScale),
				Math.round(130 * window.xScale), Math.round(25 * window.yScale));
		bLeaderBoard.setFont(font);
		bLeaderBoard.addActionListener(this);
		bLeaderBoard.addMouseListener(this);
		bCredits.setBounds(Math.round(200 * window.xScale), Math.round(500 * window.yScale),
				Math.round(130 * window.xScale), Math.round(25 * window.yScale));
		bCredits.setFont(font);
		bCredits.addActionListener(this);
		bCredits.addMouseListener(this);
		bQuit.setBounds(Math.round(225 * window.xScale), Math.round(530 * window.yScale),
				Math.round(130 * window.xScale), Math.round(25 * window.yScale));
		bQuit.setFont(font);
		bQuit.addActionListener(this);
		bQuit.addMouseListener(this);
		
		//Adds all the buttons to the panel.
		this.add(bNewGame);
		this.add(bContinue);
		this.add(bInstructions);
		this.add(bOptions);
		this.add(bLeaderBoard);
		this.add(bCredits);
		this.add(bQuit);
		this.add(fontTitle1);
		this.add(fontTitle2);
		this.add(background);
		
		//Loads the configuration.
		ArrayList<Object> config;
		try {
			config = logic.BrickBreakerConfig.load(window.userName);
			//If the sound check box was enabled.
			if((Boolean) config.get(0)) {
				//If the music is stopped.
				if(window.player == null) {
					//Plays the sound.
					window.player = new Mp3Player("res/Nepgear.mp3");
					window.player.start();
				}
			}
		} catch (NoSaveException e) {
			//By default if there's no save.
			//If the music is stopped.
			if(window.player == null) {
				//Plays the sound.
				window.player = new Mp3Player("res/Nepgear.mp3");
				window.player.start();
			}
		}
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource().equals(bNewGame)) {
			Integer lifesNumber, ammoNumber, rowNumber, columnNumber, ballSpeed, batSpeed, bonusSpeed;
			PercentNumber percent;
			try {
				//loads the Config.dat and redirects to the gaming panel with those options.
				ArrayList<Object> config = logic.BrickBreakerConfig.load(window.userName);
				lifesNumber = (Integer) config.get(1);
				ammoNumber = (Integer) config.get(2);
				rowNumber = (Integer) config.get(3);
				columnNumber = (Integer) config.get(4);
				ballSpeed = (Integer) config.get(5);
				batSpeed = (Integer) config.get(6);
				bonusSpeed = (Integer) config.get(7);
				percent = (PercentNumber) config.get(8);
			} catch (NoSaveException e1) {
				//if the file doesn't exist generates default configuration.
				lifesNumber = new Integer(3);
				ammoNumber = new Integer(0);
				rowNumber = new Integer(5);
				columnNumber = new Integer(20);
				ballSpeed = new Integer(3);
				batSpeed = new Integer(4);
				bonusSpeed = new Integer(3);
				percent = PercentNumber.TWENTY_FIVE;
			}
			window.add(new BrickBreakerGamingPanel(window, false, lifesNumber.intValue(), 0, ammoNumber.intValue(),
					rowNumber.intValue(), columnNumber.intValue(), ballSpeed.intValue() * 2, batSpeed.intValue() * 2, 
					bonusSpeed.intValue() * 2, percent, 1, 1));
			this.setVisible(false);
			window.remove(this);			
		} else if(e.getSource().equals(bContinue)) {
			//Redirects to the gaming panel with the load flag set to true. So all those zeroes means nothing.
			window.add(new BrickBreakerGamingPanel(window, true, 0, 0, 0, 0, 0, 0, 0, 0, PercentNumber.ZERO, 0, 0));
			this.setVisible(false);
			window.remove(this);
		} else if(e.getSource().equals(bInstructions)) {
			//Redirects to instructions panel.
			window.add(new BrickBreakerInstructionsPanel(window));
			this.setVisible(false);
			window.remove(this);
		} else if(e.getSource().equals(bOptions)) {
			//Redirects to options panel.
			window.add(new BrickBreakerOptionsPanel(window, "MainMenu"));
			this.setVisible(false);
			window.remove(this);
		} else if(e.getSource().equals(bLeaderBoard)) {
			//Redirects to leader board panel.
			window.add(new BrickBreakerLeaderBoardPanel(window, "MainMenu"));
			this.setVisible(false);
			window.remove(this);
		} else if(e.getSource().equals(bCredits)) {
			//Redirects to credits panel.
			window.add(new BrickBreakerCreditsPanel(window));
			this.setVisible(false);
			window.remove(this);
		} else if(e.getSource().equals(bQuit)) {
			//If the music is being playing.
			if(window.player != null) {
				//Stops the music.
				window.player.stopPlayer();
			}
			window.player = null;
			//Redirects to the game selector panel.
			window.add(new GameSelectorPanel(window));
			this.setVisible(false);
			window.remove(this);
		}
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		//Increases the buttons size when the mouse enters them.
		if(e.getSource().equals(bNewGame)) {
			logic.Utilities.buttonEffect(bNewGame, 5, window.xScale, window.yScale);
		} else if(e.getSource().equals(bContinue)) {
			logic.Utilities.buttonEffect(bContinue, 5, window.xScale, window.yScale);
		} else if(e.getSource().equals(bInstructions)) {
			logic.Utilities.buttonEffect(bInstructions, 5, window.xScale, window.yScale);
		} else if(e.getSource().equals(bOptions)) {
			logic.Utilities.buttonEffect(bOptions, 5, window.xScale, window.yScale);
		} else if(e.getSource().equals(bLeaderBoard)) {
			logic.Utilities.buttonEffect(bLeaderBoard, 5, window.xScale, window.yScale);
		} else if(e.getSource().equals(bCredits)) {
			logic.Utilities.buttonEffect(bCredits, 5, window.xScale, window.yScale);
		} else if(e.getSource().equals(bQuit)) {
			logic.Utilities.buttonEffect(bQuit, 5, window.xScale, window.yScale);
		}
	}

	@Override
	public void mouseExited(MouseEvent e) {
		//Decreases the buttons size when the mouse enters them.
		if(e.getSource().equals(bNewGame)) {
			logic.Utilities.buttonEffect(bNewGame, -5, window.xScale, window.yScale);
		} else if(e.getSource().equals(bContinue)) {
			logic.Utilities.buttonEffect(bContinue, -5, window.xScale, window.yScale);
		} else if(e.getSource().equals(bInstructions)) {
			logic.Utilities.buttonEffect(bInstructions, -5, window.xScale, window.yScale);
		} else if(e.getSource().equals(bOptions)) {
			logic.Utilities.buttonEffect(bOptions, -5, window.xScale, window.yScale);
		} else if(e.getSource().equals(bLeaderBoard)) {
			logic.Utilities.buttonEffect(bLeaderBoard, -5, window.xScale, window.yScale);
		} else if(e.getSource().equals(bCredits)) {
			logic.Utilities.buttonEffect(bCredits, -5, window.xScale, window.yScale);
		} else if(e.getSource().equals(bQuit)) {
			logic.Utilities.buttonEffect(bQuit, -5, window.xScale, window.yScale);
		}
	}

	@Override
	public void mousePressed(MouseEvent e) {
		
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		
	}
	
	@Override
	public void mouseClicked(MouseEvent e) {
		
	}
}
